package siwi.map.android;

import siwi.map.android.R;
import siwi.map.android.R.anim;

import com.wmw.Utility.Common;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class InputBox{
	public static int _MaxCntWords = 50;
	public static final int _DefaultPadding = 20;
	Context _context;
	EditText _etContent;
	ImageView _ivClearRestaurantName;
	TextView _leftCnt;
	int _paddingRight;
	String _regularExpression;
	InputBoxBehaviour _ibib;
	public InputBox(Context c, EditText et, ImageView iv, TextView leftCnt, int rightPadding, String regularExpression, InputBoxBehaviour ibib){
		_context = c;
		_etContent = et;
		_ivClearRestaurantName = iv;
		_leftCnt = leftCnt;
		_paddingRight = rightPadding;
		_regularExpression = regularExpression;
		_ibib = ibib;
		_etContent.addTextChangedListener(_twRestaurantName);
		
		if (_ibib == null)
			_ibib = new ShakeBox();
		
		_etContent.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				_ibib.Action(_etContent, _regularExpression, _context, v, hasFocus);
				_ibib.OnFocusChange(isValide(), hasFocus);
			}
		});
		
		if (_ivClearRestaurantName != null)
			_ivClearRestaurantName.setOnClickListener(_oclRestaurantName);
		
		setLeftCount();
	}
	
	OnClickListener _oclRestaurantName = new OnClickListener() {
		@Override
		public void onClick(View v) {
//			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) _etRestaurantName.getLayoutParams();
//			params.rightMargin = 0;
//			_etRestaurantName.setLayoutParams(params);
			_etContent.setText("");
		}
	};
	
	
	TextWatcher _twRestaurantName = new TextWatcher() {
		private int editStart;
		private int editEnd;

		public void afterTextChanged(Editable s) {
			editStart = _etContent.getSelectionStart();
			editEnd = _etContent.getSelectionEnd();

			// 先去掉监听器，否则会出现栈溢出
			_etContent.removeTextChangedListener(_twRestaurantName);

			// 注意这里只能每次都对整个EditText的内容求长度，不能对删除的单个字符求长度
			// 因为是中英文混合，单个字符而言，calculateLength函数都会返回1
			int len = calculateLength(s.toString());
			if (len == 0) {
				Log.v(Common.GetLineInfo(), "len - " + len);
				_paddingRight = hideClearImageView(_ivClearRestaurantName, _etContent);
			} else if (_paddingRight != 0){
				showClearImageView(_ivClearRestaurantName, _etContent, _paddingRight);
				_paddingRight = 0;
			}
			
			while ( len > _MaxCntWords) { // 当输入字符个数超过限制的大小时，进行截断操作
				s.delete(editStart - 1, editEnd);
				editStart--;
				editEnd--;
				len--;
			}
			
			_etContent.setText(s);
			_etContent.setSelection(editStart);

			// 恢复监听器
			_etContent.addTextChangedListener(_twRestaurantName);

			setLeftCount();
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};
	public boolean isValide(){
		return _etContent.getText().toString().matches(_regularExpression);
	}
	
	public void setContent(String content){
		_etContent.setText(content);
	}
	
	public String getContent(){
		return _etContent.getText().toString();
	}
	
	private int hideClearImageView(ImageView iv, EditText et){
		if (iv != null) iv.setVisibility(View.GONE);
		int prevPaddingRight = 0;
		if (et != null){
			prevPaddingRight = et.getPaddingRight();
			_etContent.setPadding(_etContent.getPaddingLeft(), _etContent.getPaddingTop(), 0, _etContent.getPaddingBottom());
		}
		return prevPaddingRight;
	}
	
	private void showClearImageView(ImageView iv, EditText et, int rightPadding){
		if (iv != null) iv.setVisibility(View.VISIBLE);
		if (_etContent != null){
			_etContent.setPadding(_etContent.getPaddingLeft(), _etContent.getPaddingTop(), rightPadding, _etContent.getPaddingBottom());	
		}
	}
	
	/**
	 * 刷新剩余输入字数,最大值新浪微博是140个字，人人网是200个字
	 */
	private void setLeftCount() {
		if (_leftCnt != null)
			_leftCnt.setText(String.valueOf((_MaxCntWords - getInputCount())));
	}
	/**
	 * 获取用户输入的分享内容字数
	 * 
	 * @return
	 */
	private int getInputCount() {
		return calculateLength(_etContent.getText().toString());
	}

	/**
	 * 计算分享内容的字数，一个汉字=两个英文字母，一个中文标点=两个英文标点 注意：该函数的不适用于对单个字符进行计算，因为单个字符四舍五入后都是1
	 * 
	 * @param c
	 * @return
	 */
	private int calculateLength(CharSequence c) {
		double len = 0;
		for (int i = 0; i < c.length(); i++) {
			int tmp = (int) c.charAt(i);
			if (tmp > 0 && tmp < 127) {
				len += 0.5;
			} else {
				len++;
			}
		}
		return (int) Math.round(len);
	}

	public interface InputBoxBehaviour {
		public void Action(EditText _etContent, String regularExpression, Context context, View v, boolean hasFocus);
		public void OnFocusChange(boolean valide, boolean hasFocus);
	}

	
	public static class ShakeBox implements InputBoxBehaviour{
		@Override
		public void Action(EditText et, String regularExpression, Context context, View v, boolean hasFocus){
			Log.v(Common.GetLineInfo(), "onFocusChange - " + hasFocus);
			String curText = et.getText().toString();
			Log.i(Common.GetLineInfo(), "current text - " + curText);
			if (!hasFocus && !curText.matches(regularExpression)) {
				Animation shakeAnim = AnimationUtils.loadAnimation(context,R.anim.shake_x);
				et.startAnimation(shakeAnim);
			}
		}

		@Override
		public void OnFocusChange(boolean valide, boolean hasFocus){
		}
	}
	
	public static class NoAnyAction implements InputBoxBehaviour{

		@Override
		public void Action(EditText _etContent, String regularExpression,
				Context context, View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void OnFocusChange(boolean valide, boolean hasFocus) {
			// TODO Auto-generated method stub
			
		}

	}
	
}
